package com.century.sjt.util;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

/**
 * Created by 炳基 on 2016/7/5.
 */
public class LocationUtil {

    public static boolean openGPSSettings(Activity myActivity) {
        LocationManager alm = (LocationManager) myActivity
                .getSystemService(Context.LOCATION_SERVICE);
        if (alm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//            Toast.makeText(myActivity, "GPS模块正常", Toast.LENGTH_SHORT)
//                    .show();
            return true;
        }

//        Toast.makeText(myActivity, "请开启GPS！", Toast.LENGTH_SHORT).show();
//        Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
//        myActivity.startActivityForResult(intent,0); //此为设置完成后返回到获取界面
        return false;
    }

    public static Location getLocation(Context mContext){
        // 获取位置管理服务
        Location mLocation;
        LocationManager locationManager;
        String serviceName = mContext.LOCATION_SERVICE;
        locationManager = (LocationManager) mContext.getSystemService(serviceName);
        // 查找到服务信息
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

        String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
        mLocation = locationManager.getLastKnownLocation(provider);
//        Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
//        updateToNewLocation(location);
//        // 设置监听器，自动更新的最小时间为间隔N秒(1秒为1*1000，这样写主要为了方便)或最小位移变化超过N米
//        locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
//                locationListener);
        return mLocation;
    }


}
